package m202401.d20;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/20 14:46
 */
//530. 二叉搜索树的最小绝对差
public class Q530 {

    private int min = Integer.MAX_VALUE;
    private int pre = -1;

    public int getMinimumDifference(TreeNode root) {
        min = Integer.MAX_VALUE;
        travel(root);
        return min;
    }

    public void travel(TreeNode node) {
        if (node == null) {
            return;
        }
        travel(node.left);
        if (pre != -1) {
            min = Math.min(min, node.val - pre);
        }
        pre = node.val;
        travel(node.right);
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
